package com.bzchao.security.component;

import com.bzchao.security.service.CustomUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;

import javax.sql.DataSource;

/*
    Bean初始化类，解决循环依赖问题
    所需的某些Bean单独在此类中初始化
*/
@Configuration
public class BeanInitializer {

    @Autowired
    DataSource dataSource;

    /**
     * 密码加密器
     */
    @Bean
    public PasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 实例化一个TokenStore，他的实现是InMemoryTokenStore，会把OAuth授权的token保存在内存中
     *
     * @return 207
     */
    @Bean("tokenStore")
    public TokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
    }

}
